/**
 * ReportGear(2011)
 */
package net.abbp.report.view.grid;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;

import javax.swing.event.MouseInputAdapter;

import net.abbp.report.view.GridView;

import com.reportgear.report.view.cursor.CursorUtils;
import com.reportgear.report.view.grid.selection.GridSelection;
import com.reportgear.report.view.grid.util.GraphHelper;

/**
 * Grid左上角控件
 * 
 * @version 1.0 2011-3-14
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 * 
 */
public class GridCornor extends BaseGridComponent {
    private static final long serialVersionUID = 4927695881175254629L;

    /**
     * Grid左上角鼠标动作处理
     */
    private class GridCornerMouseHandler extends MouseInputAdapter {
        public GridCornerMouseHandler() {

        }

        /**
         * 选择所有单元格
         */
        private void doSelectAll() {
            final GridSelection gridSelection = gridView.getGridSelection();
            gridSelection.setType(GridSelection.Type.CELL);
            gridSelection.setEditBounds(0, 0, 1, 1);
            gridSelection.setOnlySelection(0, 0, gridView.getColumnCount(), gridView.getRowCount());

            gridView.refresh();
            gridView.fireGridSelectionChanged();
        }

        @Override
        public void mouseClicked(MouseEvent evt) {
            if (isEnabled()) {
                this.doSelectAll();
            }
        }

        @Override
        public void mouseMoved(MouseEvent mouseEvent) {
            if (isEnabled()) {
                setCursor(CursorUtils.CURSOR_DEFAULT);
            }
        }
    }

    public GridCornor(GridView girdView) {
        super(girdView);

        this.setOpaque(true);
        GridCornerMouseHandler mouseHandler = new GridCornerMouseHandler();
        this.addMouseListener(mouseHandler);
        this.addMouseMotionListener(mouseHandler);

        this.getInputMap().clear();
        this.getActionMap().clear();
    }

    @Override
    public Dimension getPreferredSize() {
        if (this.gridView.isColumnHeaderVisible() || this.gridView.isRowHeaderVisible()) {
            return new Dimension(this.gridView.getGridRowHeader().getWidth(), this.gridView.getGridColumnHeader()
                    .getHeight());
        } else {
            return new Dimension(0, 0);
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        final Graphics2D g2 = (Graphics2D) g;
        final Dimension dim = this.getSize();
        final Rectangle2D client = new Rectangle2D.Double(0.0D, 0.0D, dim.getWidth(), dim.getHeight());

        g2.setPaint(this.getBackground());
        GraphHelper.fill(g2, client);
        g2.setColor(this.gridView.getGridColumnHeader().getSeparatorLineColor());
        GraphHelper.drawLine(g2, 0.0D, 0.0D, 0.0D, dim.getHeight());
        g2.setColor(this.gridView.getGridRowHeader().getSeparatorLineColor());
        GraphHelper.drawLine(g2, 0.0D, 0.0D, dim.getWidth(), 0.0D);
    }
}
